/* FIGURE 2 IN COSAR, GRIECO AND TINTELNOT (2014) 
Inputs: 
- danish_data_9596.dta
- german_data_9596.dta
Outputs: 
- figure2left.eps
- figure2left.png
- figure2right.eps
- figure2right.png
*/

clear
global path "C:\Users\Felix Tintelnot\Dropbox\WindBorder\RestatCGTReplication"

/********************************
left panel: Vestas market share
********************************/
use "$path\Data\output_files\danish_data_9596.dta" 
append using "$path\Data\output_files\german_data_9596.dta" 
rename distance_Vestas distance
sort distance
qui summarize distance,detail
save temp.dta,replace

// polynomial terms
gen distance2=distance^2
gen distance3=distance^3

// polyomial fit
regress vestas distance distance2 distance3
predict yhat1
keep yhat1 distance
save temp2.dta,replace
clear

use temp

// local averages 
xtile bins = distance, nq(15)
sort bins
by bins: egen localdistance=mean(distance)
by bins: egen localnumprojects=count(distance)
by bins: egen localvestas=sum(vestas)
gen localvestasmarketshare = localvestas/localnumprojects
duplicates drop localvestasmarketshare localdistance,force
keep localvestasmarketshare localdistance

append using temp2

// label variables 
label variable localvestasmarketshare "Quantile averages"
label variable yhat1 "Fitted values"

// Vestas market share fit with aggregated bins
twoway (scatter localvestasmarketshare localdistance) (line yhat1 distance, lcolor(maroon) lwidth(medthick)), ytitle(Market share, margin(medium)) yscale(range(0 1)) ///
ylabel(#5) xtitle(Distance to producer (km)) xlabel(0(75)1050, angle(vertical)) ///
legend(row(2) ring(0) pos(2)) plotregion(fcolor(white)) graphregion(fcolor(white))

graph export "$path\SummaryStatistics\figure2left.eps", replace
graph export "$path\SummaryStatistics\figure2left.png", replace

erase temp.dta
erase temp2.dta

/************************************
right panel: RDD of Danish producers
************************************/
use "$path\Data\output_files\danish_data_9596.dta" 
append using "$path\Data\output_files\german_data_9596.dta" 
gen     country = 0
replace country = 1 if germany==1
replace distance_border = -distance_border if denmark == 1
keep distance_border country prodc_denmark

sort country distance_border

// trim data
summarize distance_border,detail
drop if distance_border>r(p99)
drop if distance_border<r(p1)
summarize distance_border

// polynomial terms
gen distance_border2=distance_border^2
gen distance_border3=distance_border^3

// separate discontinuity regressions by country (separate polynomials) 
gen interact = distance_border*country
gen interact2 = distance_border2*country
gen interact3 = distance_border3*country

// data regression discontinuity
regress prodc_denmark distance_border* interact* country, robust level(99)  
predictnl yhat=predict(), ci(yhat_lc yhat_uc) 

gen yhat_den = yhat if country==0
gen yhat_ger = yhat if country==1

save temp.dta,replace
clear
use temp

// local averages in Denmark
keep if country==0
xtile bins = distance_border, nq(4)
sort bins
by bins: egen localdistance=mean(distance_border)
by bins: egen localnumprojects=count(distance_border)
by bins: egen localdanish=sum(prodc_denmark)
gen localdanishmarketshare = localdanish/localnumprojects
duplicates drop localdanishmarketshare localdistance,force
rename localdanishmarketshare localdanishmarketshare_dnk
rename localdistance localdistance_dnk
keep localdanishmarketshare_dnk localdistance_dnk

append using temp
save temp2.dta,replace
clear

// local averages in Germany
use temp
keep if country==1
xtile bins = distance_border, nq(8)
sort bins
by bins: egen localdistance=mean(distance_border)
by bins: egen localnumprojects=count(distance_border)
by bins: egen localdanish=sum(prodc_denmark)
gen localdanishmarketshare = localdanish/localnumprojects
duplicates drop localdanishmarketshare localdistance,force
rename localdanishmarketshare localdanishmarketshare_ger
rename localdistance localdistance_ger
keep localdanishmarketshare_ger localdistance_ger

append using temp2

//label variable localvestasmarketshare "Quantile averages"
label variable yhat_ger "Fitted values"
label variable yhat_den "Fitted values"
label variable localdanishmarketshare_dnk "Quantile averages"
label variable localdanishmarketshare_ger "Quantile averages"

// data regression discontinuity fit with aggregated bins
twoway (scatter localdanishmarketshare_dnk localdistance_dnk,mcolor("navy")) ///
(scatter localdanishmarketshare_ger localdistance_ger,mcolor("navy") msize("medlarge")) ///
(line yhat_den yhat_ger distance_border, xline(0) lcolor(maroon maroon) lwidth(medthick medthick)), ///
ytitle(Market share, margin(medium)) yscale(range(0 1)) ylabel(#5) xtitle(Distance to border (km)) ///
xlabel(-300(75)675 0, angle(vertical)) note((Distance is negative for Denmark, positive for Germany), ///
size(vsmall) position(6)) legend(row(2) order(1 3) ring(0) pos(2)) plotregion(fcolor(white)) graphregion(fcolor(white))

graph export "$path\SummaryStatistics\figure2right.eps", replace
graph export "$path\SummaryStatistics\figure2right.png", replace

erase temp.dta
erase temp2.dta
